﻿using PMIS.Shared.Models;
using PMIS.WebApi.Model;
using PMIS.WebApi.Repository;
using SqlSugar;
using System.Linq.Expressions;

namespace PMIS.WebApi.Services
{
    public class BaseService<TEntity> : IBaseService<TEntity> where TEntity : class, new()
    {
        //数据仓储【autofac属性依赖注入】
        public ISugarRepository<TEntity> SugarRepository { get; set; }

        public ISqlSugarClient SugarClient => SugarRepository.SugarClient;


        #region 新增

        public async Task<int> AddEntityAsync(TEntity entity)
        {
            return await SugarRepository.AddAsync(entity);
        }

        public async Task<int> AddEntityListAsync(List<TEntity> entityList)
        {
            return await SugarRepository.AddAsync(entityList);
        }

        #endregion

        #region 修改

        public async Task<int> UpdateEntityAsync(TEntity entity, List<string> lstIgnoreColumns = null, bool isLock = true)
        {
            return await SugarRepository.UpdateAsync(entity, lstIgnoreColumns, isLock);
        }

        public async Task<int> UpdateEntityListAsync(List<TEntity> entityList)
        {
            return await SugarRepository.UpdateAsync(entityList);
        }

        #endregion

        #region 删除

        public async Task<int> DeleteEntityAsync<T>(Expression<Func<T, bool>> exp) where T : class, IDeletedFilter, new()
        {
            return await SugarClient.Updateable<T>()
                .SetColumns(it => new T() { IsDeleted = true },
                    true) //true 支持更新数据过滤器赋值字段一起更新
                .Where(exp).ExecuteCommandAsync();
        }

        #endregion

        #region Queryable

        public ISugarQueryable<TEntity> Table => SugarClient.Queryable<TEntity>();

        public async Task<TEntity> GetEntityAsync(string id)
        {
            return await Table.InSingleAsync(id);
        }

        public virtual async Task<PagingModel<TEntity>> GetEntityListAsync(int? pageNum, int? pageSize, Expression<Func<TEntity, bool>>? predicate = null)
        {
            ISugarQueryable<TEntity> queryable = Table;
            if (predicate != null)
            {
                queryable = Table.Where(predicate);
            }

            RefAsync<int> totalCount = 0;

            List<TEntity> items;

            if (pageNum.HasValue && pageSize.HasValue)
            {
                // Paginated query
                items = await queryable.ToPageListAsync(pageNum.Value, pageSize.Value, totalCount);
            }
            else
            {
                // Non-paginated query, return all items
                items = await queryable.ToListAsync();
                totalCount = items.Count;
            }

            return new PagingModel<TEntity>
            {
                Count = totalCount,
                Items = items
            };
        }

        /// <summary>
        /// Table
        /// </summary>
        /// <param name="whereExpression">条件</param>
        /// <param name="orderExpression">排序表达式</param>
        /// <param name="orderByType">排序方式</param>
        /// <returns></returns>
        public ISugarQueryable<TEntity> QueryEntity(Expression<Func<TEntity, bool>> whereExpression = null,
            Expression<Func<TEntity, object>> orderExpression = null, OrderByType? orderByType = null)
        {
            orderByType ??= OrderByType.Asc;
            return Table.WhereIF(whereExpression != null, whereExpression)
                .OrderByIF(orderExpression != null, orderExpression, (OrderByType)orderByType);
        }

        #endregion
    }
}
